<?php
namespace Common\Logic;

use Think\Auth;

/**
 * 权限扩展验证
 */
class AuthCheckLogic {
    protected $me = array();
    public function __construct()
    {
        $this->me = D('User')->getMe();
    }

    /**
     * 登录权限
     * @param $_auth
     * @return bool
     */
    public function login_auth($_auth)
    {
        $UserDao = D('Common/User');
        if ($_auth && !$UserDao->isLogin()) {
            redirect(U('User/Login/index'));
        }
    }

    /**
     * 控制器权限
     * @param $_auth
     * @return bool
     */
    public function ctrl_auth ($_auth) {
        if($this->isAdministrator()){
            return true;
        }
        $authDao = new Auth();
        $name = __INFO__;
        if(C('URL_HTML_SUFFIX')) {
            $name = preg_replace('/\.(' . trim(C('URL_HTML_SUFFIX'), '.') . ')$/i', '', __INFO__);
        }
        if(!$authDao->check($name, $this->me['id'], array('exp', '<>""'))) {
            E('抱歉，您的权限不足!');
        }
    }
    public function isAdministrator() {
        $UserDao = D('Common/User');
        $me = $UserDao->getLocalSession();
        // 超级管理员 跳过所有验证
        if ($UserDao->isAdministrator($me['id'])) {
            return true;
        }
        return false;
    }
}